#include "up.h"
#include "../../core/settings.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtGui/QImage>
#include <QtCore/QFile>
#include <QtGui/QMessageBox>
#include <QtNetwork/QNetworkCookieJar>
#include <QtNetwork/QNetworkCookie>
#include <shotscreenswebapi.h>
#include <shotscreenswebapi.h>
#include <QInputDialog>
#include <QHBoxLayout>

HttpSystem::HttpSystem(QObject *parent) :
        QObject(parent)
{
    //settings.setPath(QSettings::IniFormat,QSettings::UserScope,"ShotScreensUploader.ini");
}

void HttpSystem::GetPluginVersion(int &version, int &subversion)
{
    version=1;
    subversion=0;
}

void HttpSystem::OnFinish(bool result)
{
    switch(my_api -> GetCurrentAPIState())
    {
        case NO:
            return;
        case LOGIN:
            if(result)
            {
                my_api -> GetFolderList();
            }
            else
            {
                MessageBox.setModal(true);

                MessageBox.critical(NULL, QString(""), QString("Login failed!"), QMessageBox::Ok, QMessageBox::NoButton);
            }
            break;
        case CREATE_FOLDER:
             break;
    case GET_FOLDER_LIST_STATE:
            if(result)
            {
                imageProperty.ClearFolderList();
                imageProperty.SetAPIHandle(my_api);
                imageProperty.SetImageData(image_data);
                imageProperty.folderNames=my_api->folderNames;
                imageProperty.folderHashes=my_api->folderHashes;
                imageProperty.FillFolders();
                imageProperty.setModal(true);
                imageProperty.show();
            }


            break;
    case UPLOAD_FILE_STATE:
            if(result)
            {
                my_api -> Logout();
                MessageBox.information(NULL, QString(""), QString("Image was uploaded!"), QMessageBox::Ok, QMessageBox::NoButton);
            }

            break;
    case LOGOUT_STATE:
            my_api->deleteLater();
            deleteLater();
            break;

    }
}

void HttpSystem::GetPluginName(QString &dest_buffer)
{
    dest_buffer.clear();
    dest_buffer.append("Image uploader to ShotScreens web-site");
}

bool HttpSystem::UploadFile(QString fileName)
{
    return false;
}

bool HttpSystem::UploadStream(QByteArray &data)
{
    if(login.length() == 0 && password.length() == 0)
    {
        login = Settings::instance().getLogin();
        password = Settings::instance().getPassword();
    }
    my_api = new ShotScreensWebAPI(0,login,password);

    image_data.clear();
    if((login.length() == 0) && password.length() == 0)
    {
        MessageBox.setModal(true);
        MessageBox.information(NULL, QString(""), QString("Please setup user name and password first!"), QMessageBox::Ok, QMessageBox::NoButton);
        settingsWindow.LoadSettings();
        settingsWindow.setModal(true);
        settingsWindow.show();
        return false;
    }
    image_data = data;

    connect(my_api, SIGNAL(OnFinish(bool)), SLOT(OnFinish(bool)));

    my_api->Login();
    return true;
}

void HttpSystem::SetLoginAndPassword(QString login, QString password)
{
    Q_UNUSED(login);
    Q_UNUSED(password);
}

void HttpSystem::ShowOptions(QDialog* settingsWindowDraft)
{
    QHBoxLayout* layout = new QHBoxLayout;
    layout->addWidget(&settingsWindow);
    settingsWindowDraft->setLayout(layout);
    settingsWindowDraft->setModal(true);
    settingsWindowDraft->show();
}

Q_EXPORT_PLUGIN2(UPLOADER, HttpSystem)
